import json
import re
import time
import sys

from config import getConfigParams
from requestUrl import getDocByUrl,getEntryIdByUrl
from formatContent import formatContent, filterTag
from logs import saveLog

# url = 'https://36kr.com/p/897082081538819'

# 获取配置参数
configParams = getConfigParams(sys.argv)
url = configParams['real_url']
env = configParams['env']

# 获取文章ID
entryId = getEntryIdByUrl(configParams['base_url'])

headers = {
    'referer': 'https://36kr.com',
}

# 获取html文本doc，得到pq对象
doc = getDocByUrl(url, headers)
# 过滤标签
doc = filterTag(doc, ['.item-time i'])

# 封面图
coverUrl = re.findall("<meta.*?property=\"og:image\" content=\"([^\"]+)\"/>", doc.html())[0]

# 标题
title = doc('.article-title').text()

# 文章摘要
description = doc('.description').text()

# 文章正文
articleContent = doc('.articleDetailContent')

# 文章作者
authorName = doc('.author-detail-info .author-name').text()

# 时间
# 需要移除<i>标签，因为里面是一个“·”
publicTime = re.findall("<meta.*?name=\"popintimestamp\" content=\"([^\"]+)\"/>", doc.html())[0]

# 图片来源声明
imageStatement = doc('.footer-image_source')

if imageStatement:
    imageStatement = imageStatement.html()
else:
    imageStatement = ''
# 需要将文章内容和图片来源声明进行拼装
content = articleContent.html() + imageStatement

# 去【李世泉】那里过滤标签
content = formatContent(content,env=env)

result = {
    'entry_id': entryId,
    'not_format_content': articleContent.html() + imageStatement,
    'content': content,
    'author_name': authorName,
    'cover_url': coverUrl,
    'public_time': publicTime,
    'description': description,
    'title': title,

}
logData = {
    'params': sys.argv,
    'result': result
}
saveLog(json.dumps(logData, ensure_ascii=False))
print(json.dumps(result, ensure_ascii=False))
